import {
  _decorator,
  Component,
  Label,
  Node,
  Sprite,
  resources,
  SpriteFrame,
  Widget,
  Layout,
} from "cc";
import { Card } from "./Card";
import { CardManager } from "./CardManager";
import { Player } from "./Player";
import { PokerAction } from "./Action";

const { ccclass, property } = _decorator;

@ccclass("PlayerManager")
export class PlayerManager extends Component {
  // 空座位
  @property(Node)
  emptySeatNode: Node = null;

  // 头像
  @property(Node)
  avatarNode: Node = null;

  // 昵称
  @property(Label)
  nicknameLabel: Label = null;

  // 筹码
  @property(Node)
  moneyNode: Node = null;

  // 手牌1
  @property(Node)
  cardNode1: Node = null;

  // 手牌2
  @property(Node)
  cardNode2: Node = null;

  // 庄位
  @property(Sprite)
  dealearNode: Sprite = null;

  // action
  @property(Node)
  chipNode: Node = null;

  start() {}

  update(deltaTime: number) {}

  public showEmptySeat() {
    this.emptySeatNode.active = true;
    this.avatarNode.active = false;
    this.nicknameLabel.node.active = false;
    this.moneyNode.active = false;
    this.chipNode.active = false;
  }

  public showUserInfo(player: Player, isDealer: boolean, index: number) {
    this.emptySeatNode.active = false;

    this.avatarNode.active = true;

    this.nicknameLabel.node.active = true;
    this.nicknameLabel.string = player.username;

    this.moneyNode.active = true;
    const moneyLabel = this.moneyNode
      .getChildByName("Label")
      .getComponent(Label);
    moneyLabel.string = player.chips.toString();

    if (isDealer) {
      this.dealearNode.node.active = true;
    } else {
      this.dealearNode.node.active = false;
    }

    this.showAction(player.action, player.bet, index);
  }

  public async showCards(cards: Card[]) {
    if (cards.length != 2) {
      console.error("=====> 手牌数量!=两张");
      return;
    }
    await this.showHandCard(cards[0], this.cardNode1);
    await this.showHandCard(cards[1], this.cardNode2);
  }

  async showHandCard(card: Card, cardNode: Node) {
    const cardManager = cardNode.getComponent(CardManager);
    cardManager.setCard(card);
    cardManager.showBack();
    await cardManager.flipToFront();
  }

  public showAction(action: number, amount: number, index: number) {
    const chipWidget = this.chipNode.getComponent(Widget);
    const chipLayout = this.chipNode.getComponent(Layout);
    const chipTypeSprite = this.chipNode
      .getChildByName("ChipType")
      .getComponent(Sprite);
    const amountLabel = this.chipNode
      .getChildByName("Amount")
      .getComponent(Label);

    if (index < 5) {
      chipWidget.isAlignLeft = false;
      chipWidget.isAlignRight = true;
      chipWidget.right = -42;
      chipLayout.horizontalDirection = Layout.HorizontalDirection.LEFT_TO_RIGHT;
    } else {
      chipWidget.isAlignLeft = true;
      chipWidget.isAlignRight = false;
      chipWidget.left = -42;
      chipLayout.horizontalDirection = Layout.HorizontalDirection.RIGHT_TO_LEFT;
    }

    // 设置动作图片名
    var chipSpriteFrameName = "";

    switch (action) {
      case PokerAction.smallBlind: // 小盲
        chipSpriteFrameName = "chip_sb";
        break;
      case PokerAction.bigBlind: // 大盲
        chipSpriteFrameName = "chip_bb";
        break;
      default:
        chipSpriteFrameName = "chip"; // 默认不显示
        break;
    }
    if (amount > 0) {
      const frame =
        chipTypeSprite.spriteAtlas.getSpriteFrame(chipSpriteFrameName);
      chipTypeSprite.spriteFrame = frame;
      amountLabel.string = amount.toString();
      this.chipNode.active = true;
    } else {
      this.chipNode.active = false;
    }
  }

  public canJoinSeat(): boolean {
    return this.emptySeatNode.active;
  }
}
